/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2022-10-22
 * Time: 20:55
 */

import java.util.LinkedList;
import java.util.Queue;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
class Solution {
    public boolean isCompleteTree(TreeNode root) {
        TreeNode cur=root;
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(cur);
        //用来标志是否出现过null
        boolean flag=false;
        while(!queue.isEmpty()){
            int size=queue.size();
            for(int i=0;i<size;i++){
                TreeNode temp=queue.poll();
                if(temp!=null){
                    if(flag){
                        //如果出现过null并且还有其他节点,直接false
                        return false;
                    }
                    queue.offer(temp.left);
                    queue.offer(temp.right);
                }else{
                    flag=true;
                }
            }
        }
        return true;
    }
}